const net = require('net')

const server = net.createServer()

server.listen('9877', '192.168.43.49');
server.on('listening', function() {
	console.log('[server:onlistening]', arguments)
})

server.on('connection', function(proxySocket) {
	console.log('[server:onconnection]', arguments);

	mqSocket = net.connect('9876', '192.168.43.49')
	mqSocket.on('error', function() {
		console.log('[mqSocket:onerror]', arguments)
	})

	mqSocket.on('connect', function() {
		console.log('[mqSocket:onconnect]', arguments)

		mqSocket.on('data', function(data) {
			console.log('[mqSocket:ondata]', data.toString('utf-8'))
			console.log('[mqSocket:ondata]', data.toString('base64'))
			proxySocket.write(data)
		})

	})

	mqSocket.on('end', function() {
		console.log('[mqSocket:onend]', arguments)
	})
	mqSocket.on('close', function() {
		console.log('[mqSocket:onclose]', arguments)
	})

	
	proxySocket.on('data', function(data) {
		console.log('[server:proxySocket:ondata]', data.toString('utf-8'))
		console.log('[server:proxySocket:ondata]', data.toString('base64'))
		console.log('[server:proxySocket:ondata]', data.toString('hex'))
		console.log('\n')

		console.log('[mqSocket:write] data: ', data.toString('utf-8'))
		console.log('[mqSocket:write] data: ', data.toString('base64'))
		console.log('[mqSocket:write] data: ', data.toString('hex'))
		console.log('\n')
		mqSocket.write(data)
	})
	proxySocket.on('close', function(){
		console.log('[server:proxySocket:onclose]', arguments)
		mqSocket.end();
	})

})


server.on('close', function() {
	console.log('[server:onclose]', arguments)
})

